<?php
/**
 * @file messageprivate.php $
 * @author LiangJian (liangjian@haibao.com) $
 * @date 2015年10月20日 下午1:41:05 $
 * @brief 报友私信【这里只包含私人消息相关信息】 $
 * 相关接口文档：http://faq.comsenz.com/library/UCenter/interface/interface_pm.htm
 */
namespace haibao\www\business\message;

use haibao\frame\http\HttpEnvironment;
use haibao\www\business\BusinessException;

class MessagePrivate extends \haibao\www\business\Base{
    
    /**
     * 获取未读私人消息数量
     */
    public static function getNewMessage($userId){
        if(!$userId){
            throw new BusinessException('用户ID错误');
        }
        self::loadClient();
        
        $return = uc_pm_checknew($userId, 2);
        return (int)$return['newprivatepm'];
    }
    
    /**
     * 获取报友私信消息列表
     */
    public static function getPrivateMessage($userId, $page = 1, $pagesize = 15){
        if(!$userId){
            throw new BusinessException('用户ID错误');
        }
        self::loadClient();
        
        $messageList = uc_pm_list($userId, $page, $pagesize, 'inbox', 'privatepm', 0);
        if($messageList['count']){
            $msgList = $messageList['data'];
            $msgIdArr = array();
            foreach($msgList as $message){
                array_push($msgIdArr, $message['plid']);
            }
            self::updateMessageStatus($userId, $msgIdArr);
        }
        
        return $messageList;
    }
    
    /**
     * 发送/回复 报友私信
     */
    public static function sendMessage($fromUid, $toUid, $message, $replypid = 0){
        if(!$fromUid || !$toUid || !$message){
            throw new BusinessException('回复私信参数错误');
        }
        self::loadClient();
        
        return uc_pm_send($fromUid, $toUid, null, $message, 1, $replypid, 0, 0);
        
    }
    /**
     * 获取会话的消息内容数据
     */
    public static function getMessageView($userId,$toUserId,$pmid=0,$daterange=5,$page=1,$pagesize=100,$type=0,$isplid=0){
    	self::loadClient();
    	return uc_pm_view($userId,$pmid,$toUserId,$daterange,$page,$pagesize,$type,$isplid);
    }
    
    /**
     * 更改消息标记状态
     */
    public static function updateMessageStatus($userId, $plids, $status = 0){
        self::loadClient();
        
        return uc_pm_readstatus($userId, array(), $plids, $status);
    }
    private static function loadClient(){
        require_once(HttpEnvironment::path().'/sites/common/third/uc_client/config_global.php');
        require_once(HttpEnvironment::path().'/sites/common/third/uc_client/config_ucenter.php');
        require_once(HttpEnvironment::path().'/sites/common/third/uc_client/client.php');
    }
    
}